package com.eras.shoes.utils;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;

import com.eras.shoes.contents.CalibrateBean;
import com.eras.shoes.contents.Global;
import com.eras.shoes.contents.MessageEvent;
import com.eras.shoes.contents.ShoesPressBack;
import com.eras.shoes.contents.SmartShoesWebBean;
import com.eras.shoes.ui.CalibrateActivity;

import org.greenrobot.eventbus.EventBus;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class ShoeDataThread extends Thread {
    private ArrayList<SmartShoesWebBean> mShoeDataList;
    private volatile boolean mbRuning=false;
    public CalibrateActivity mCalibrateActivity;
    public CalibrationObject calibrationObject;
    public volatile List<CalibrateBean.press> press;
    private final Handler mHandler = new Handler(Looper.getMainLooper());
    private final Object lock = new Object(); // 用于线程同步
    public void StartThread(){
        super.start();
    }
    public void StopThread(){
        mbRuning=false;
//        DataComputingManage.DateQueue.clear();
        interrupt();
    }

    // 启动/重启线程
    public void startThread(List<CalibrateBean.press> p) {
//        if (!mbRuning) {
//            mbRuning = true;
//            if (getState() == State.TERMINATED) {
//                // 如果线程已终止，需要新建实例
//                new ShoeDataThread().start();
//            } else {
//                start();
//            }
//        }
        mbRuning = true;
        this.press = p;
    }

    public void setData(List<CalibrateBean.press> p) {
        this.press = p;
    }
    // 主线程调用的更新方法
    public void updatePressData(List<CalibrateBean.press> newPress) {
        this.press.clear();
        this.press = new ArrayList<>();
        this.press = newPress;
    }

    public ShoeDataThread(CalibrateActivity activity, List<CalibrateBean.press> newPress)
    {
        mbRuning=false;
        calibrationObject=null;
//        calibrationDataObject=null;
//        mSmartShoesEvaluateActivity=null;
        mCalibrateActivity= activity;
        mShoeDataList=new ArrayList<SmartShoesWebBean>();
        this.press = newPress;
    }

    public void AddShoesData(SmartShoesWebBean sData){
        if(!mbRuning) return;
//        int nDataCount=0;
        synchronized (mShoeDataList){
            mShoeDataList.add(sData);
            mShoeDataList.notify();
        }

    }

    @Override
    public void run() {
        mbRuning=true;
        int nLastCount=0;
        while(mbRuning) {
            SmartShoesWebBean sDataBean=null;
            synchronized (mShoeDataList){
                try{
                    mShoeDataList.wait(300);
                    if(!mbRuning) return;
                    nLastCount=mShoeDataList.size();
                    if(nLastCount>0){
                        sDataBean=mShoeDataList.get(0);
                        mShoeDataList.remove(0);
                    }
                }
                catch (Exception ex){
                    ex.printStackTrace();
                }

            }

            if(sDataBean!=null) {
//                long startTime=System.currentTimeMillis();
//                DataComputingObject dataComputingObject = new DataComputingObject(sDataBean.getBulDate(), calibrationObject);
                if (press != null && press.size() == 7) {
                    // 处理press数据
                    if (mCalibrateActivity != null) {
                        DataComputingObject dataComputingObject = new DataComputingObject(sDataBean.getBulDate(), calibrationObject);
                        Float computingResult = DataComputingManage.getComputingResult(dataComputingObject, press);
                        int value = Math.round(computingResult);
//                        BigDecimal bd = new BigDecimal(computingResult);
//                        int intValue = bd.intValue();  // 结果为123
                        if (computingResult <=0) {
                            computingResult = 0f;
                        }
                        double press = sDataBean.getBulDate().dataList.get(28);
                        double pressCount = press - Global.P0;
                        double airPress = Global.airPress;
                        if (pressCount <= 0) {
                            pressCount = 0;
                        }
                        ShoesPressBack shoesPressBack = new ShoesPressBack(computingResult, press, pressCount, airPress);
                        EventBus.getDefault().post(new MessageEvent<>(MessageEvent.SMART_SHOES_PRESS_DATA_BACK, shoesPressBack));

//                        mCalibrateActivity.setDataToView(intValue);
                    }
                }
//                mCalibrateActivity.setDataToView(String.valueOf(sDataBean.getBulDate().dataList.get(28)));

                //Log.i("评估 - ", JSONObject.toJSONString(dataComputingObject));
//                ComputingResult computingResult =

//                long currTime=System.currentTimeMillis();
//                long runTime=currTime-startTime;
//                currTime-=sDataBean.bulDate.mRecvTimestamp;
//                Log.i("计算结果 ","type: "+sDataBean.getType()+" RecvTime:"+sDataBean.bulDate.mRecvTimestamp +
//                        "  duration:"+currTime+" MainThread Time: "+runTime+" devTime:"+sDataBean.bulDate.timestamp+" data："+sDataBean.getBulDate().dataList.get(28)+" ,weight:"+computingResult.weight+" ,LastCount:"+nLastCount);


//                Log.i("CalibrateActivity", "computingResult="+sDataBean.getBulDate().dataList.get(28));
//                long currTime=System.currentTimeMillis();
//                long runTime=currTime-startTime;
//                currTime-=sDataBean.bulDate.mRecvTimestamp;
//                Log.i("计算结果 ","type: "+sDataBean.getType()+" RecvTime:"+sDataBean.bulDate.mRecvTimestamp +
//                        "  duration:"+currTime+" MainThread Time: "+runTime+" devTime:"+sDataBean.bulDate.timestamp+" data："+sDataBean.getBulDate().dataList.get(28)+" ,weight:"+computingResult.weight+" ,LastCount:"+nLastCount);
            }
        }
    }



}
